<template>
  <router-view class="view" />
</template>

<script>
import { getUser, getMenus } from "./api";
import _import from "./scripts/_import";
import { handleMenus } from "./scripts/utils";
import { setTimeout } from "timers";
// import Bus from './bus'

export default {
  name: "app",
  data() {
    return {
    };
  },
  mounted() {
  },
  created() {
    this.init();
    this.$bus.$on("gitMenuBus", () => {
      this.init();
    });
  },
  methods: {
    async assembleFrame() {
      const deploy = window.PUZZLE_CONFIG.deploy;
      const host = window.PUZZLE_CONFIG.host;
      const version = window.PUZZLE_CONFIG.version;
      let frame;
      if (deploy == "single") {
        frame = await _import(
          "frames",
          localStorage.getItem("frame") || PUZZLE_CONFIG.frame
        );
      } else {
        frame = await _import("frames", info.name, host, version);
      }
      // const frame = await _import("frames", info.name, info.host, info.version);
      // 路由注册
      this.$router.addRoutes(frame.routerStatic);
      // Store 注册
      /*let token = sessionStorage.getItem("token");
      let _this = this
      if (token) {
        for (let name in frame.store) {
            console.log(_this.$store.state);
            console.log(_this.$store.state.tabs);
          if (!_this.$store.state[name]) {
            this.$store.registerModule(name, frame.store[name]);
          }
          // this.$store.registerModule(name, frame.store[name]);
        }
      }*/
      // 嵌套路由 / 默认两级路由
      return frame.routerStatic.filter((item) => item.path == "/");
    },
    async assemblePuzzles(menus, childRouter) {
      let pages = childRouter[0].children;
      // 尝试获取模块 / 异步获取
      for (let puzzle of menus)
        _import("puzzles", puzzle.label, "./puzzles", "0.1.0")
          .then((p) => {
            // 需要生成路由的菜单
            let menusRouter = [];
            if (puzzle.children && puzzle.children.length == 0) {
              menusRouter.push(puzzle);
            } else {
              handleMenus(puzzle.children, menusRouter);
            }
            // 路由
            childRouter[0].children = p
              .router(menusRouter, puzzle.label)
              .concat(p.routerStatic);
            pages.push(...childRouter[0].children);
            this.$router.addRoutes(childRouter);
            // Store
            for (let name in p.store)
              this.$store.registerModule(name, p.store[name]);
          })
          .catch((err) => {});
      // 储存路由表
      this.$store.commit("SET_PAGES", pages);
    },
    async init() {
      document.title = PUZZLE_CONFIG.appName;
      let token = await sessionStorage.getItem("token");
      // 组装模块
      if (this.$route.path == "/login") {
        sessionStorage.removeItem("token");
        sessionStorage.removeItem("refresh_token");
        sessionStorage.removeItem("username");
        sessionStorage.removeItem("userId");
        sessionStorage.removeItem("permissions");
        sessionStorage.removeItem("isTipPassUpdate");
        sessionStorage.removeItem("tabList");
      }
      //   if (this.$route.path != "/login") {
      if (token) {
        // 获取数据
        const [user, menus] = await Promise.all([getUser(), getMenus()]);
        this.$store.commit("SET_USER", user);
        let childRouter = await this.assembleFrame();
        if(user){
          this.$store.commit("SET_MENUS", menus);
          this.assemblePuzzles(menus.data, childRouter);
        }
      } else {
        // window.location.reload()
        // // 组装基座并获取基座嵌套路由
        let childRouter = await this.assembleFrame();
        // this.assemblePuzzles(menus.data, childRouter);
      }
    },
  },
};
</script>
